###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/ModifyExtraField.pm.pl -> ModifyExtraField module              #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::ModifyExtraField;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";
require "Variables.pm.pl";

use strict;

require "ControlPanel/Output/ModifyExtraField.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, ERROR => "", @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #

  &Authenticate::CP(DB => $in{'DB'}, ACTION => "EXTRAFIELDS_MODIFY");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  if (!$SL::QUERY{'FID'}) {
    &Error::Error("CP", MESSAGE => "You didn't specify an Extra Field ID (FID)");
  }
  
  my $ExtraField;
  foreach my $field (@{ $GENERAL->{'RESOURCE_EXTRA'} }) {
    $ExtraField = $field if ($field->{'ID'} eq $SL::QUERY{'FID'});
  }
  
  unless ($ExtraField) {
    &Error::Error("CP", MESSAGE => "The Extra Field ID (FID) you specified is invalid");
  }

  my %INPUT;
  
  $INPUT{'EXTRAFIELD'} = $ExtraField;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::ModifyExtraField->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT, error => $in{'ERROR'});
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #

  &Authenticate::CP(DB => $in{'DB'}, ACTION => "EXTRAFIELDS_MODIFY");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  if (!$SL::QUERY{'FID'}) {
    &Error::Error("CP", MESSAGE => "You didn't specify an Extra Field ID (FID)");
  }
  
  my $ExtraField;
  foreach my $field (@{ $GENERAL->{'RESOURCE_EXTRA'} }) {
    $ExtraField = $field if ($field->{'ID'} eq $SL::QUERY{'FID'});
  }
  
  unless ($ExtraField) {
    &Error::Error("CP", MESSAGE => "The Extra Field ID (FID) you specified is invalid");
  }

  my @Fields = (
    { name => "DESCRIPTION"   , required => 1              },
    { name => "REQUIRED"      , required => 1              },
    { name => "PUBLIC"        , required => 1              },
    { name => "SIZE"          , required => 0              },
    { name => "CUSTOM"        , required => 0              },
    { name => "CUSTOM_DONE"   , required => 0              },
    { name => "CUSTOM_DISPLAY", required => 0              },
    { name => "CHECKSUB"      , required => 0              }
  );

  my (%RECORD, %INPUT);

  my @Error;
  foreach my $field (@Fields) {
    if ($field->{'required'} && $SL::QUERY{'FORM_'.$field->{'name'}} eq "") {
      push(@Error, "MISSING-".$field->{'name'});
    } elsif ($field->{'size'} && $SL::QUERY{'FORM_'.$field->{'name'}} ne "" && length($SL::QUERY{'FORM_'.$field->{'name'}}) > $field->{'size'}) {
      push(@Error, "TOOLONG-".$field->{'name'});
    } else {
      $RECORD{ $field->{'name'} } = $SL::QUERY{'FORM_'.$field->{'name'}};
    }
  }

  if (scalar(@Error) >= 1) {
    $self->show(DB => $in{'DB'}, ERROR => \@Error);
    return 1;
  }

  #----------------------------------------------------------------------#
  # Updating data...                                                     #

  $RECORD{'ID'} = $ExtraField->{'ID'};

  foreach my $field (@{ $GENERAL->{'RESOURCE_EXTRA'} }) {
    next unless ($field->{'ID'} eq $RECORD{'ID'});
    $field = \%RECORD;
    last;
  }

  my $Variables = Variables->new();
  
  $Variables->Update(
    FILE      => "$SL::PATH/Private/Variables/General.pm.pl",
    PACKAGE   => "General",
    VARIABLE  => "GENERAL",
    VALUES    => {
      RESOURCE_EXTRA  => $GENERAL->{'RESOURCE_EXTRA'}
    }
  ) || &Error::Error("CP", MESSAGE => "Error updating variable file. $Variables->{'ERROR'}");

  $INPUT{'RECORD'}     = \%RECORD;
  $INPUT{'EXTRAFIELD'} = $ExtraField;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::ModifyExtraField->new();

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);
  
  return 1;
}

1;